package org.snail.activemq.listener;

import org.apache.log4j.Logger;
import org.snail.attendence.mapper.AttendenceMongoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@Component("abAttendenceListener")
public class AbAttendenceListener implements MessageListener {
    protected static final Logger LOGGER = Logger.getLogger(AbAttendenceListener.class);
    @Autowired
    private AttendenceMongoMapper attendenceMongoMapper;

    @Override
    public void onMessage(Message message) {
        LOGGER.info("[abAttendenceListener.onMessage]:begin onMessage.");
        TextMessage textMessage = (TextMessage) message;
        String jsonStr = null;
        try {
            jsonStr = textMessage.getText();
            System.out.println("测试:" + jsonStr);
            Thread.sleep(2000);
//        try {
//            //获取数据
//            String jsonStr = textMessage.getText();
//            LOGGER.info("[abAttendenceListener.onMessage]:receive message is,"+ jsonStr);
//            if (jsonStr != null) {
//                AbAttendenceMongo abAttendenceMongo = JSON.parseObject(jsonStr, AbAttendenceMongo.class);
//                List<AbnormalAttendanceDTO> abnormalAttendanceDTOList = null;
//                if (null == abAttendenceMongo || null == abAttendenceMongo.getAbnormalAttendanceDTOList() || abAttendenceMongo.getAbnormalAttendanceDTOList().isEmpty()) {
//                    throw new CommonException(ErrCodeConstant.ILLEGAL_PARAM, "fectch the object---abnormalAttendenceList from consumer fail,because param is null!");
//                }
//                abnormalAttendanceDTOList = abAttendenceMongo.getAbnormalAttendanceDTOList();
//                List<AbnormalAttendanceDTO> listByFlag = attendenceMongoMapper.findListByFlag(1);
//                for (AbnormalAttendanceDTO abnormalAttendanceDTO : abnormalAttendanceDTOList) {
//                    attendenceMongoMapper.saveGeneralAttendence(abnormalAttendanceDTO);
//                    attendenceMongoMapper.updateCorrect(listByFlag);     //将之前校正的考勤加入
//                }
//            }
//        } catch (JMSException e) {
//            LOGGER.error("[abAttendenceListener.onMessage]:receive message occured an exception",e);
//        } catch (CommonException e) {
//            LOGGER.error("abAttendenceListener save to mongo failed!", e);
//            e.printStackTrace();
//        }
        } catch (JMSException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
